package servlet;

import model.Blog;
import model.BlogDao;
import model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;

/**
 * @Title: addBlog
 * @Author 郑雨杭
 * @Package servlet
 * @Date 2024/7/30 15:08
 * @description:
 */
@WebServlet("/addBlog")
public class addBlog extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        HttpSession session = req.getSession(false);
        if(session==null){
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
        User user =(User) session.getAttribute("user");
        if(user==null){
            resp.sendRedirect(req.getContextPath()+"/login.html");
        }
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        if(title==null || title.length()==0 || content==null || content.length()==0){
            resp.sendRedirect(req.getContextPath()+"/blog_edit.html");
            return;
        }
        BlogDao blogDao = new BlogDao();
        Blog blog = new Blog();
        blog.setTitle(title);
        blog.setContent(content);
        //blog.setPostTime(new Timestamp(System.currentTimeMillis()));
        blog.setUserid(user.getUserid());
        //blog.setBlogid();
        blogDao.insert(blog);
        resp.sendRedirect(req.getContextPath()+"/blog_list.html");
    }
}
